package com.js.rtu.mvp.present;


import android.util.Log;

import com.js.rtu.base.BasePresenter;
import com.js.rtu.mvp.bean.base.CommonBean;
import com.js.rtu.util.BleUtils;

import java.util.Locale;

/**
 * 11查询终端时钟
 * Created by wz on 2019/5/17.
 */
public class Cmd11Presenter implements BasePresenter<String> {


    @Override
    public CommonBean<String> paseData(String msg) {
        CommonBean<String> bean = new CommonBean<>();
        int index = 0;
        String[] data = msg.split(" ");
        if (data[0].equals("EE") && data[1].equals("EE")
                && data[data.length - 1].equals("FF")
                && data[data.length - 2].equals("FF")) {

            //帧头、CRC、帧尾
            bean.setStart(data[0] + data[1]);
            bean.setCRC(data[data.length - 4] + data[data.length - 3]);
            bean.setEnd(data[data.length - 1] + data[data.length - 2]);
            index = 2;//2字节

            //address
            StringBuilder str_address = new StringBuilder();
            for (int i = 0; i < 8; i++) {
                str_address.append(data[index + i]);
            }
            index += 8;//8字节
            String address = BleUtils.hexStr2Str(str_address.toString());
            bean.setAddress(address);


            //length固定长度2，hex
            int length = BleUtils.hToint(data[index + 1] + data[index]);
            index += 2;//2字节
            bean.setLength(length);


            //cmd固定长度1
            int cmd = Integer.parseInt(data[index]);
            index += 1;//1字节
            bean.setCmd(cmd);

            //上次自报时间
            String str_time = "20" +
                    BleUtils.hToint(data[index]) + "/" +
                    String.format(Locale.CHINA, "%02d",
                            BleUtils.hToint(data[index + 1])) +
                    "/" +
                    String.format(Locale.CHINA, "%02d",
                            BleUtils.hToint(data[index + 2])) +
                    " " +
                    String.format(Locale.CHINA, "%02d",
                            BleUtils.hToint(data[index + 3])) +
                    ":" +
                    String.format(Locale.CHINA, "%02d",
                            BleUtils.hToint(data[index + 4])) +
                    ":" +
                    String.format(Locale.CHINA, "%02d",
                            BleUtils.hToint(data[index + 5]));
            bean.setData(str_time);
            index += 6;//6字节

        }

        return bean;

    }
}





